package net.messagenet.mazor.ejb.facade;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.ejb.Stateless;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;

import net.messagenet.mazor.DO.CustomerDO;
import net.messagenet.mazor.DO.DpaDO;
import net.messagenet.mazor.DO.DpaProductDO;
import net.messagenet.mazor.DO.PosDO;
import net.messagenet.mazor.DO.PosProductDO;
import net.messagenet.mazor.exception.MZRApplicativeException;
import net.messagenet.mazor.exception.MZRInternalServerException;
import net.messagenet.mazor.infra.InfrastructureConstants;
import net.messagenet.mazor.infra.cache.UserCacheManager;
import net.messagenet.mazor.infra.engine.EngineInterface;

import org.apache.log4j.Logger;
import org.jboss.mx.util.MBeanProxyExt;
import org.jboss.mx.util.MBeanServerLocator;

/**
 * Session Bean implementation class MZRFacade
 */
@Stateless
public class MZRFacade implements MZRFacadeLocal {

	
	private Logger logger = Logger.getLogger(this.getClass());
    /**
     * Default constructor. 
     */
	
	private EngineInterface eng;
    public MZRFacade() {
        
    }
    
    
    private EngineInterface getEngine() throws MZRInternalServerException{
    	if (eng==null){
        	MBeanServer server = MBeanServerLocator.locate();
        	try {
    			EngineInterface eng = (EngineInterface) MBeanProxyExt.create(
    					EngineInterface.class,InfrastructureConstants.ENGINE_JMX_SERVICE_NAME,
    					server);
    			return eng;
    		} catch (MalformedObjectNameException e) {
    			throw new MZRInternalServerException("Error finding Mbean", e);
    		}
    	}else
    		return eng;

    }
    @Override
    public void createDPARequest(DpaDO dpa) throws MZRInternalServerException,MZRApplicativeException{
    	logger.debug("create new dpa request");
    	getEngine().createDPARequest(dpa);
    }


	@Override
	public void saveDPADraft( DpaDO dpa)
			throws MZRInternalServerException, MZRApplicativeException {
    	logger.debug("create draft dpa request");
    	getEngine().saveDPADraft(dpa);
		
	}


	@Override
	public UserCacheManager getUserCacheManager() throws MZRInternalServerException {
		logger.debug("getting cache manger");
		return getEngine().getCacheManager();
	}


	@Override
	public void loadDPAAnswerSheet( String path)
			throws MZRInternalServerException, MZRApplicativeException {
		logger.debug("reading the reutrning DPA excel file");
		getEngine().loadDPAAnswerSheet(path);
		
	}


	@Override
	public Map<String,DpaDO> getAllApprovedDPAs()
			throws MZRInternalServerException, MZRApplicativeException {
    	logger.debug("getting all the open dpas");
    	return getEngine().getAllApprovedDPAs();
	}


	@Override
	public void createNewPOSRequest( PosDO posDo) throws MZRInternalServerException, MZRApplicativeException {
		logger.debug("saving new POS request");
		getEngine().createNewPOSRequest(posDo);
		
	}


	@Override
	public void createOrEditCustomer( CustomerDO customer) throws MZRInternalServerException {
		logger.debug("creating new customer");
		getEngine().createOrEditCustomer(customer);
		
	}


	@Override
	public List<DpaProductDO> getDPAReportedProducts(Date fromDate, Date toDate) throws MZRInternalServerException {
		logger.debug("getting all dpa reported products");
		return getEngine().getDPAReportedProducts(fromDate, toDate);
		
	}


	@Override
	public void updatePaidProducts( List<DpaProductDO> products) throws MZRInternalServerException {
		logger.debug("updating the products paid quantity");
		getEngine().updatePaidProducts(products);
		
	}


	@Override
	public List<DpaDO> getDPADrafts() throws MZRInternalServerException {
		logger.debug("getting all dpa drafts");
		return getEngine().getDPADrafts();
	}


	@Override
	public void removeDPAProducts( List<DpaProductDO> removePrds) throws MZRInternalServerException {
		logger.debug("removing proudcts");
		getEngine().removeDPAProducts(removePrds);
		
	}


	@Override
	public List<PosDO> getPOSDrafts()
			throws MZRInternalServerException {
		logger.debug("getting all pos drafts");
		return getEngine().getPOSDrafts();
	}


	@Override
	public void savePOSDraft( PosDO pos)
			throws MZRInternalServerException {
    	logger.debug("create draft dpa request");
    	getEngine().savePOSDraft(pos);
		
	}


	@Override
	public void removePOSProducts( List<PosProductDO> removePrds)
			throws MZRInternalServerException {
		logger.debug("removing pos proudcts");
		getEngine().removePOSProducts(removePrds);
		
	}


	@Override
	public Map<Integer, DpaDO> getAllNotClosedDPAs()
			throws MZRInternalServerException {
		logger.debug("getting all not closed dpas");
		return getEngine().getAllNotClosedDPAs();
	}


	@Override
	public List<DpaDO> getClosedAndNotApprovedDPAs() throws MZRInternalServerException {
    	logger.debug("getting all the closed and not approved dpas");
    	return getEngine().getClosedAndNotApprovedDPAs();
	}


	@Override
	public Map<Integer, DpaDO> getAllDPAs() throws MZRInternalServerException {
    	logger.debug("getting all dpas");
    	return getEngine().getAllDPAs();
	}


}
